package com.nexgo.payment.trans.setting.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.nexgo.payment.App;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.util.SPUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import static android.telephony.TelephonyManager.SIM_STATE_READY;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 状态上送操作业务
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年07月12日
 */

public class ResetBasePresenter implements ResetBaseContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(ResetBasePresenter.class.getSimpleName());
    private List<String> mBaseList = null;
    private ResetBaseContract.View mView;
    private final String BASEINFO = "base_info";

    public ResetBasePresenter(@NonNull ResetBaseContract.View view, @NonNull DataSource repository) {
        mView = checkNotNull(view);
        mBaseList = new ArrayList<>();
        mView.setPresenter(this);
    }

    private List<String> getAllBaseModeInfo(){
        List<CellInfo> infoLists;
        List<String> baseInfoList = null;
        StringBuilder bytes = new StringBuilder();
        TelephonyManager telephonyManager = (TelephonyManager) App.sContext.getSystemService(Context.TELEPHONY_SERVICE);
        if(telephonyManager != null && telephonyManager.getSimState() == SIM_STATE_READY){
            Log.d("getAllBaseModeInfo","telephonyManager != null");
            infoLists = telephonyManager.getAllCellInfo();
            if(infoLists != null && infoLists.size() > 0) {
                baseInfoList = new ArrayList<>();
                Log.d("getAllBaseModeInfo", "infoLists.size: " + infoLists.size());
                for (int i = 0; i < infoLists.size(); i++) {
                    CellInfo cellInfo = infoLists.get(i);
                    if (cellInfo instanceof CellInfoGsm) {
                        CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
                        CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();

                        bytes.append("MCC:");
                        bytes.append(cellIdentityGsm.getMcc());
                        bytes.append("|");

                        bytes.append("MNC:");
                        bytes.append(cellIdentityGsm.getMnc());
                        bytes.append("|");

                        bytes.append("CID:");
                        bytes.append(cellIdentityGsm.getCid());
                        bytes.append("|");

                        bytes.append("LAC:");
                        bytes.append(cellIdentityGsm.getLac());
                        bytes.append("|");
                        baseInfoList.add(bytes.toString());
                    } else if (cellInfo instanceof CellInfoCdma) {  //CMDA电信
                        CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
                        CellIdentityCdma cellIdentityCdma = cellInfoCdma.getCellIdentity();

                        bytes.append("MCC:");
                        bytes.append(telephonyManager.getNetworkOperator().substring(0, 3));
                        bytes.append("|");

                        bytes.append("MNC:");
                        bytes.append(cellIdentityCdma.getSystemId());
                        bytes.append("|");

                        bytes.append("CID:");
                        bytes.append(cellIdentityCdma.getBasestationId());
                        bytes.append("|");

                        bytes.append("LAC:");
                        bytes.append(cellIdentityCdma.getNetworkId());
                        bytes.append("|");
                        baseInfoList.add(bytes.toString());
                    } else if (cellInfo instanceof CellInfoWcdma) {
                        CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
                        CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();

                        bytes.append("MCC");
                        bytes.append(cellIdentityWcdma.getMcc());
                        bytes.append("|");

                        bytes.append("MNC:");
                        bytes.append(cellIdentityWcdma.getMnc());
                        bytes.append("|");

                        bytes.append("CID:");
                        bytes.append(cellIdentityWcdma.getCid());
                        bytes.append("|");

                        bytes.append("LAC:");
                        bytes.append(cellIdentityWcdma.getLac());
                        bytes.append("|");
                        baseInfoList.add(bytes.toString());
                    } else if (cellInfo instanceof CellInfoLte) {
                        CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                        CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();

                        bytes.append("MCC:");
                        bytes.append(cellIdentityLte.getMcc());
                        bytes.append("|");

                        bytes.append("MNC:");
                        bytes.append(cellIdentityLte.getMnc());
                        bytes.append("|");

                        bytes.append("CID:");
                        bytes.append(cellIdentityLte.getCi());
                        bytes.append("|");

                        bytes.append("LAC:");
                        bytes.append(cellIdentityLte.getTac());
                        bytes.append("|");
                        baseInfoList.add(bytes.toString());
                    }
                }
            }
        }
        return baseInfoList;
    }

    @Override
    public void start() {
        mBaseList = getAllBaseModeInfo();
        if(mBaseList != null){
            mLog.debug("mBaseList {}",mBaseList.size());
            saveBaseInfo(mBaseList);
            mView.showTransResult(true);
        }
        else{
            mView.showTransResult(false);
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void saveBaseInfo(List<String> data) {
        SPUtil mSpUtil =  SPUtil.getInstance(App.sContext,BASEINFO);
        mSpUtil.cleanSharedPreferences();
        mSpUtil.putList(App.sContext,BASEINFO,mBaseList);
    }


    @Override
    public List<String> getBaseInfo() {
        SPUtil mSpUtil =  SPUtil.getInstance(App.sContext,BASEINFO);
        List<String> base = mSpUtil.getList(App.sContext,BASEINFO);
        mLog.debug("base.size {}",base.size());
        return null;
    }
}
